package com.zys.el.time.mq.handler;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zys.el.time.advice.entity.CommentAdvice;
import com.zys.el.time.advice.service.CommentAdviceService;
import com.zys.el.time.constants.RedisConstants;
import com.zys.el.time.entity.Comment;
import com.zys.el.time.mq.dto.MqDTO;
import com.zys.el.time.service.CommentService;
import com.zys.el.time.service.impl.ThinkingServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.ZoneOffset;

@Slf4j
@Component
public class CommentHandler {

    @Resource
    private CommentService commentService;
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ThinkingServiceImpl thinkingService;
    @Resource
    private CommentAdviceService commentAdviceService;

    public void save(MqDTO mqDTO) {
        Comment comment = JSON.parseObject(mqDTO.getData(), Comment.class);
        log.debug(String.valueOf(comment));
        // 审核
        boolean b = true;
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 设置审核状态
        if (b) comment.setStatus(1);
        else comment.setStatus(3);

        // 入库
        commentService.save(comment);

        // 设置Redis
        // 获取锁
        RLock lock = redissonClient.getLock(RedisConstants.COMMENT_LOCK_KEY + comment.getCommentId());
        if (lock.tryLock()) {
            String key = RedisConstants.COMMENT_KEY + comment.getCommentId();
            stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(comment));
            lock.unlock();
        }

        boolean isReplied = false;
        String commentPageKey;
        if (comment.getRootCommentId() != null && comment.getReplieeId() != null) {
            isReplied = true;
            commentPageKey = RedisConstants.PAGE_REPLIED_KEY + comment.getCommentId();
        } else {
            commentPageKey = RedisConstants.PAGE_COMMENT_KEY + comment.getThinkingId();
        }

        // 评论分页缓存
        long milli = comment.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        stringRedisTemplate.opsForZSet().addIfAbsent(commentPageKey, String.valueOf(comment.getCommentId()), milli);

        // 通知
        CommentAdvice commentAdvice;
        if (isReplied) { // 二级评论
            commentAdvice = getCommentAdvice(comment, comment.getReplieeId());
        } else { // 一级评论
            Long thinkingId = comment.getThinkingId();
            // 帖子的用户
            Long userId = thinkingService.getBaseMapper().getUserIdByThinkingId(thinkingId);
            commentAdvice = getCommentAdvice(comment, userId);
        }

        // 通知入库
        commentAdviceService.save(commentAdvice);

        // 通知分页缓存
        String commentAdviceKey = RedisConstants.ADVICE_KEY + commentAdvice.getToUserId();
        stringRedisTemplate.opsForZSet().addIfAbsent(commentAdviceKey, String.valueOf(comment.getCommentId()), milli);


    }

    private CommentAdvice getCommentAdvice(Comment comment, Long toUserId) {
        CommentAdvice commentAdvice = new CommentAdvice();
        commentAdvice.setFromUserId(comment.getUserId());
        commentAdvice.setToUserId(toUserId);
        commentAdvice.setCommentId(comment.getCommentId());
        commentAdvice.setType(2); // 评论
        commentAdvice.setStatus(1); // 未读
        commentAdvice.setMsg(comment.getText());
        return commentAdvice;
    }


}
